// pages/song-detail/song-detail.js
import rankingStore from "../../store/rankingStore"
import recommendStore from "../../store/recomment"
import playerStore from "../../store/playerStore"
import {reqgetRecommendList} from "../../service/homeMusic"
const app = getApp()
Page({
  data: {
    type: "",
    key: "",
    id: "",
    songInfo: {},
    scrollHeight: 300,
    menuHeight: 0
  },
  onLoad(options) {
    // 1.确定获取数据的类型
    // type: ranking -> 榜单数据
    // type: recommend -> 推荐数据
    const type = options.type
    // this.data.type = type
    this.setData({ type })

    // 获取store中榜单数据
    if (type === "ranking") {
      const key = options.key
      this.data.key = key
      rankingStore.onState(key, this.handleRanking)
    }else if(type === "recommend"){
      recommendStore.onState("recommendList", this.handleRanking)
    }else if (type === "menu"){
      const id = options.id
      this.data.id = id
      this.getRecommendList()
      this.setData({menuHeight: 225})
    }
    this.setData({ scrollHeight: app.globalData.screenHeight - this.data.menuHeight })
  },
  async getRecommendList(){
    const res = await reqgetRecommendList(this.data.id)
    this.setData({ songInfo: res.playlist })
  },

  handleRanking(value) {
    // if (this.data.type === "recommend") {
    //   value.name = "推荐歌曲"
    // }
    this.setData({ songInfo: value })
    wx.setNavigationBarTitle({
      title: value.name,
    })
  },
  // ================== wxml事件监听 ==================
  onSongItemTap(event) {
    const index = event.currentTarget.dataset.index
    playerStore.setState("playSongList", this.data.songInfo.tracks)
    playerStore.setState("playSongIndex", index)
  },

  onUnload(){
    if (this.data.type === "ranking") {
      rankingStore.offState(this.data.key, this.handleRanking)
    } else if (this.data.type === "recommend") {
      recommendStore.offState("recommendList", this.handleRanking)
    }
  }
})